define(['request','template','jquery'],(req,template) => {
    class Header{
        constructor(){
            this.init()
        }
        init(){
            // 加载头尾部
            $('header').load('/html/modules/header.html',() => {
                //头部加载完成后调用
                this.allCount()
                this.loginBtn()
                this.regBtn()
            })
            this.getTabs()
            
        }
        loginBtn(){
            var glBtn = document.querySelector('.loginBtn')
            var login = document.querySelector('.l-g-c')
            // console.log(glBtn)
            // console.log(login)
            glBtn.onclick = function(){
                let e = e || window.event
                login.style.display = 'block'
                e.stopPropagation();
            }
            document.onclick=function () {
                   login.style.display="none";
                //    console.log("隐藏了");
            }; 
        }
        // addLoginData(){
        // }
        regBtn(){
            var regBtn = document.querySelector('.registerBtn')
            var register = document.querySelector('.l-c-g')
            // console.log(regBtn)
            // console.log(register)
            regBtn.onclick = function(){
                let e = e || window.event
                register.style.display = 'block'
                e.stopPropagation();
            }
            document.onclick=function () {
                register.style.display="none";
                //    console.log("隐藏了");
            }; 
        }

         //获取导航栏 
        getTabs(){
            req.getIndexTab()
                .then(res => {
                    // console.log(res)
                    //渲染导航栏
                    if(res.code === 200){
                        const myData = res.data.list.slice(1,6)
                        // console.log(myData)
                        $('.h-m-t').html(template('tabsTemplate',{list:myData}))
                    }
                })
        }
        //计算购物车商品总量
        allCount(){
            //从localStorage 里面把数据计算出来
            let prodList = localStorage.getItem('prodList')
            let count = 0

            if(prodList){
                prodList = JSON.parse(prodList)
                prodList.forEach(item => {
                    count += item.count
                })
            }
            //找到元素进行设置
            $('.h-lbtn').text(count)
        }
    }
    return new Header()
})
